package com.tcbj.springbootmail.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import sun.management.FileSystem;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;


/**
 * <br>
 * 标题: 邮件服务<br>
 * 描述: 邮件服务<br>
 * 时间: 2018/10/19<br>
 *
 * @author 杰梵
 */
@Service
public class MailService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     *  发送简单文本邮件
     * @param to          收件人
     * @param subject     邮件标题
     * @param content     邮件内容
     */
    public void sendSimpleMail(String to, String subject, String content){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        mailMessage.setFrom(from);
        javaMailSender.send(mailMessage);
    }

    /**
     *  发送Html文本邮件
     * @param to          收件人
     * @param subject     邮件标题
     * @param content     邮件内容
     */
    public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        //true表示需要创建一个multipart message
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(from);
        javaMailSender.send(mailMessage);

    }

    /**
     *  发送带附件的邮件
     * @param to          收件人
     * @param subject     邮件标题
     * @param content     邮件内容
     * @param filePath    附件地址
     * @throws MessagingException
     */
    public void sendAttachmntsMail(String to, String subject, String content,String filePath) throws MessagingException {
        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        //true表示需要创建一个multipart message
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(from);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = file.getFilename();
        helper.addAttachment(fileName,file);  //可以添加多个附件
        javaMailSender.send(mailMessage);
    }

    /**
     * 发送带图片的邮件
     * @param to          收件人
     * @param subject     邮件标题
     * @param content     邮件内容
     * @param rscPath     图片地址
     * @param rscId       图片ID
     * @throws MessagingException
     */
    public void sendInlinResourceMail(String to, String subject, String content, String rscPath, String rscId)  {
        MimeMessage mailMessage = javaMailSender.createMimeMessage();

        logger.info("发送静态邮件开始：{},{},{},{},{}",to, subject, content, rscPath, rscId);
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(mailMessage,true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            helper.setFrom(from);
            FileSystemResource file = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId,file);
            helper.addInline(rscId,file);
            logger.info("发送图片邮件成功！");
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("发送静态邮件失败：",e);
        }

        javaMailSender.send(mailMessage);
    }


}
